package com.etlmaster.executor.steps.extract;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.LogWriter;
import com.etlmaster.executor.utils.Shell;
import com.etlmaster.executor.utils.StepState;

/**
 * @author Wugf
 * @date 2016-03-08
 */
public class ExtractUtil {
		public static File writeToFile(String content, String suff, String parentPath)
				throws IOException {
			int i = 0;
			File f = new File(parentPath, new Date().getTime() + "_" + i + "_"
					+  content.hashCode() + "." + suff);
			while (f.exists()) {
				f = new File(parentPath, new Date().getTime() + "_" + (++i) + "_"
						 + content.hashCode() + "." + suff);
			}
			LogWriter.addLog("INFO","创建执行sh文件：{}",f.getAbsolutePath());
			f.createNewFile();
			FileOutputStream fos = new FileOutputStream(f);
			fos.write(content.toString().getBytes("UTF-8"));
			fos.close();
			return f;
		}

		public static String getLogPath() {

			String logPath = DataCollectionUtils.getProperties(
					"tempshelldir");
			if (null == logPath || logPath.equalsIgnoreCase("")) {
				logPath = System.getProperty("user.dir");
			}
			return logPath;
		}
		public static int execute(String cmd){
			LogWriter.addLog("INFO","执行命令：{}",cmd);
			File f = null;
			
			String logPath = getLogPath();
			try {
				f =  writeToFile(cmd.toString(), "sh", logPath);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			f.setExecutable(true);
			Shell.ShellCommandExecutor executor= new Shell.ShellCommandExecutor(new String[]{f.getPath()});
			
			try {
				executor.execute();
				return executor.getExitCode();
			} catch (IOException e) {
				
				e.printStackTrace();
				return StepState.RUNNING_FAILED;
			}
			
		}
		public static String replace(String cmd,Map<String,Object> map){
			Set keys=map.keySet();
			Iterator iterator=keys.iterator();
			while(iterator.hasNext()){
				String key=(String) iterator.next();
				
				cmd=cmd.replace(":"+key, (CharSequence) map.get(key));
			}
			return cmd;
		}
		public static String replaceBySplit(String cmd,Map<String,Object> map,String split){
			if(null==split)
				split="";
			Set keys=map.keySet();
			Iterator iterator=keys.iterator();
			while(iterator.hasNext()){
				String key=(String) iterator.next();
				
				cmd=cmd.replace(split+key, (CharSequence) map.get(key));
			}
			return cmd;
		}
		public static Map<String,Object> getMap(Object meta){
			Map<String,Object> map=new HashMap<String,Object>();
			Class cls=meta.getClass();
			Field[] fields=cls.getDeclaredFields();
			for(int i=0;i<fields.length;i++)
			{
				try {
					fields[i].setAccessible(true);
					map.put(fields[i].getName(), ((StepMeta) meta).variableSubstitution((String) fields[i].get(meta)));
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return map;
		}
		public static Map<String,String> getMap_(Object meta){
			Map<String,String> map=new HashMap<String,String>();
			Class cls=meta.getClass();
			Field[] fields=cls.getDeclaredFields();
			for(int i=0;i<fields.length;i++)
			{
				try {
					fields[i].setAccessible(true);
					map.put(fields[i].getName(), ((StepMeta) meta).variableSubstitution((String) fields[i].get(meta)));
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return map;
		}
}
